#!/bin/sh /etc/rc.common

USE_PROCD=1
START=91

append_arg() {
	local cfg="$1"
	local var="$2"
	local opt="$3"
	local def="$4"
	local val

	config_get val "$cfg" "$var"
	[ -n "$val" -o -n "$def" ] && procd_append_param command $opt "${val:-$def}"
}

append_bool() {
	local cfg="$1"
	local var="$2"
	local opt="$3"
	local def="$4"
	local val

	config_get_bool val "$cfg" "$var" "$def"
	[ "$val" = 1 ] && procd_append_param command "$opt"
}

start_instance() {
	local cfg="$1"
	local aux

	config_get_bool aux "$cfg" 'disabled' '0'
	[ "$aux" = 1 ] && return 1

	procd_open_instance

	procd_set_param command /usr/sbin/oscam
	procd_append_param command "-B /var/run/oscam.pid"

	append_arg "$cfg" debug_level "-d"
	append_arg "$cfg" pending_ecm "-p"
	append_arg "$cfg" restart_level "-r"
	append_arg "$cfg" syslog_ident "-I"
	append_arg "$cfg" temp_dir "-t"
	append_arg "$cfg" wait_on_startup "-w"

	append_bool "$cfg" show_sensitive "-S"
	append_bool "$cfg" webif_utf8 "-u"

	config_get_bool aux "$cfg" 'respawn' '0'
	[ "$aux" = 1 ] && procd_set_param respawn

	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "oscam"
}

start_service() {
	config_load oscam
	config_foreach start_instance oscam
}
